package com.minli.weixin.controller;

import com.alibaba.fastjson.JSONObject;
import com.minli.weixin.service.IWxmpService;
import com.minli.weixin.service.bo.WxmpTempMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Formatter;
import java.util.Map;

/**
 * <p>微信网关</p>
 * <p>Created by wangml on 2017/5/22.</p>
 */
@RequestMapping("/wxmp")
@Controller
public class WxmpController {
    private Logger logger = LoggerFactory.getLogger(WxmpController.class);
    @Autowired
    private IWxmpService wxmpService;

    /**
     * 发送微信消息
     * @param request
     * @param httpServletResponse
     * @param openid
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendMsg")
    public String sendMsg(HttpServletRequest request, HttpServletResponse httpServletResponse,
                                String openid) throws Exception {
        sendNotify();
        return "推送成功";
    }


    /**
     * 发送通知
     * @throws Exception
     */
    private void sendNotify() throws Exception {
        String openId = "oQuzTjtT6Zlei627xn6MXJ46cz1U";//接收消息微信用户openid
        String url = "http://www.baidu.com"; //点击通知跳转页面
        String template_id = "pytXkeXU9SGE8hI8wlaxfuI6RJImByGit3TO3rBUHcc";//微信消息模板


        //合成data信息
        JSONObject data = new JSONObject();
        JSONObject first = new JSONObject();
        first.put("value", "您好：zhangsan");
        first.put("color", "#173177");
        data.put("first", first);

        JSONObject merchantNameObj = new JSONObject();
        merchantNameObj.put("value", "福州大学");
        merchantNameObj.put("color", "#173177");
        data.put("merchantName", merchantNameObj);

        JSONObject amtObj = new JSONObject();
        amtObj.put("value", 200.00);
        amtObj.put("color", "#173177");
        data.put("amt", amtObj);

        JSONObject tradeNoObj = new JSONObject();
        tradeNoObj.put("value", "63655223365");
        tradeNoObj.put("color", "#173177");
        data.put("tradeNo", tradeNoObj);

        JSONObject titleObj = new JSONObject();
        titleObj.put("value", "付款成功通知");
        titleObj.put("color", "#173177");
        data.put("orderTitle", titleObj);

        JSONObject remarkObj = new JSONObject();
        remarkObj.put("value", "您有一笔待支付订单，请尽快点击进行支付");
        remarkObj.put("color", "#173177");
        data.put("remark", remarkObj);

        WxmpTempMsg wxmpTempMsg = new WxmpTempMsg();
        wxmpTempMsg.setOpenId(openId);
        wxmpTempMsg.setTemplateId(template_id);
        wxmpTempMsg.setData(data);
        wxmpTempMsg.setUrl(url);
        wxmpService.sendTempMsg(wxmpTempMsg);
    }



    /**
     * 字节转字符
     * @param hash
     * @return
     */
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash){
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * 重定向
     * @param url
     * @return
     */
    private ModelAndView redirect(String url){
        return new ModelAndView("redirect:"+url+"?v="+wxmpService.getVersion());
    }

    /**
     * 重定向
     * @param url
     * @param params
     * @return
     */
    private ModelAndView redirect(String url,Map<String,Object> params){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:"+url);
        modelAndView.addObject("v",wxmpService.getVersion());
        for (String key:params.keySet()){
            modelAndView.addObject(key,params.get(key));
        }
        return modelAndView;
    }
}
